package com.example.holes;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Loot {
	private final int MAX_FRAMES = 30;
	private final int TIME_BETWEEN = 2;
	Bitmap image = null;
	private final int type, holeNum;
	private float left, top;
	private int row, height, ycutoff;
	private int value;
	private int animateTimer = TIME_BETWEEN;
	private int frame = 0;
	private float topInc;
	private boolean gone = false;
	
	public Loot(int holeNum, int type, Bitmap[] images){
		this.type = type;
		this.holeNum = holeNum;
		setStats();
		
		setImagePosition(holeNum);
		setImage(images);
		this.height = image.getHeight();
		this.topInc = calcInc();
		
		// scale position for different screen sizes
		topInc = Global.SCALE_RATIO_H * topInc;
		ycutoff = (int)(Global.SCALE_RATIO_H * ycutoff);
		left = (int)(Global.SCALE_RATIO_W * left);
		top = (int)(Global.SCALE_RATIO_H * top);
	}
	
	// scales image if the loot is in the 2nd or 3rd row
	private void setImage(Bitmap[] images){
		image = images[type];
		// 2nd row: 80% width, 80% height
		if (row == 2){
			image = Bitmap.createScaledBitmap(image,(int)(image.getWidth()*0.80), (int)(image.getHeight()*0.80), false);
		}
		// 3rd row: 60% width, 60% height
		else if (row == 3){
			image = Bitmap.createScaledBitmap(image,(int)(image.getWidth()*0.60), (int)(image.getHeight()*0.60), false);
		}
	}
	
	// set the ycutoff (where image enters hole)
	// and then return the yInc to get there in MAX_FRAMES
	private float calcInc(){
		switch(row){
			case 1: ycutoff = 419; break;
			case 2: ycutoff = 287; break;
			case 3: ycutoff = 188; break;
		}
		
		return (ycutoff - top) / MAX_FRAMES;
	}
	
	// sets value of loot depending on type
	private void setStats(){
		switch(type){
			case 0:	value = 1;
					break;
			case 1: value = 10;
					break;
		}
	}
	
	// ==============
	//     UPDATE
	// ==============
	public void Update(){
		if (gone == false){
			animateTimer--;
			if (animateTimer == 0){
				frame++;
				if (frame == MAX_FRAMES)
					gone = true;
				else{
					nextFrame();
					animateTimer = TIME_BETWEEN;
				}
			}
		}
	}
	// ==============
	//     UPDATE
	// ==============
	
	// ==============
	//     DRAW
	// ==============
	public void Draw(Canvas c){
		if (image != null){
			c.drawBitmap(image, left, top, null);
		}
	}
	// ==============
	//     DRAW
	// ==============
	
	// moves loot image down--if image passes the ycutoff (enters hole)
	// then crop the image accordingly.
	private void nextFrame(){
		top += topInc;
		int topheight = (int)(top + height);
		// crop image
		if ((topheight) > ycutoff){
			image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), height - (topheight - ycutoff));
		}
	}
	
	public boolean isGone(){ return gone; }
	public int getHole(){ return holeNum; }
	public int getType(){ return type; }
	public int getValue(){ return value; }
	
	private void setImagePosition(int holeNum){
		switch (holeNum) {
			case 0:	row = 1;
					left = 202;
					top = 306;
					break;
			case 1:	row = 1;
					left = 383;
					top = 306;
					break;
			case 2:	row = 1;
					left = 564;
					top = 306;
					break;
			case 3:	row = 2;
					left = 227;
					top = 197;
					break;
			case 4:	row = 2;
					left = 386;
					top = 197;
					break;
			case 5:	row = 2;
					left = 545;
					top = 197;
					break;
			case 6:	row = 3;
					left = 246;
					top = 121;
					break;
			case 7:	row = 3;
					left = 389;
					top = 121;
					break;
			case 8:	row = 3;
					left = 532;
					top = 121;
					break;
		}
	}
}
